package com.smartleanx.module.hrm.service.companysystememployee;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.smartleanx.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.smartleanx.module.hrm.app.empRisk.EmployeeRiskHandleService;
import com.smartleanx.module.hrm.app.risk.RiskHandleService;
import com.smartleanx.module.hrm.context.EmployeeContextHolder;
import com.smartleanx.module.hrm.controller.admin.companysystememployee.vo.CompanySystemEmployeePageReqVO;
import com.smartleanx.module.hrm.controller.admin.companysystememployee.vo.CompanySystemEmployeeSaveReqVO;
import com.smartleanx.module.hrm.eums.risk.RiskTypeEnum;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.smartleanx.module.hrm.dal.dataobject.companysystememployee.CompanySystemEmployeeDO;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.companysystememployee.CompanySystemEmployeeMapper;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 公司制度已学习人员 Service 实现类
 *
 * @author 精益大师
 */
@Service
@Validated
public class CompanySystemEmployeeServiceImpl implements CompanySystemEmployeeService {

    @Resource
    private CompanySystemEmployeeMapper companySystemEmployeeMapper;
    @Resource
    @Lazy
    private RiskHandleService riskHandleService;

    @Override
    public Long createCompanySystemEmployee(CompanySystemEmployeeSaveReqVO createReqVO) {
        // 校验是否已完成学习
        List<CompanySystemEmployeeDO> learned = companySystemEmployeeMapper.getCompanySystemLearned(createReqVO.getEmployeeId(), createReqVO.getCompanySystemId());
        if (learned.size() > 0) {
            throw exception(COMPANY_SYSTEM_EMPLOYEE_EXISTS);
        }
        // 插入
        CompanySystemEmployeeDO companySystemEmployee = BeanUtils.toBean(createReqVO, CompanySystemEmployeeDO.class);
        companySystemEmployeeMapper.insert(companySystemEmployee);
        // 风险扫描
        riskHandleService.execute(Collections.singleton(companySystemEmployee.getCompanySystemId()), RiskTypeEnum.REGULATION_NOT_INFORM.getCode());
        // 返回
        return companySystemEmployee.getId();
    }

    @Override
    public List<CompanySystemEmployeeDO> getListByCompanySystemIds(Collection<Long> ids) {
        return companySystemEmployeeMapper.getListByCompanySystemIds(ids);
    }

    @Override
    public List<CompanySystemEmployeeDO> getListByEmployeeId(Collection<Long> ids) {
        return companySystemEmployeeMapper.selectList(new LambdaQueryWrapperX<CompanySystemEmployeeDO>()
                .in(CompanySystemEmployeeDO::getEmployeeId, ids));
    }

    @Override
    public PageResult<CompanySystemEmployeeDO> getCompanySystemEmployeePage(CompanySystemEmployeePageReqVO pageReqVO) {
        return companySystemEmployeeMapper.selectPage(pageReqVO);
    }

    @Override
    public void deleteByEmployeeId(Long employeeId) {
        companySystemEmployeeMapper.delete(new LambdaQueryWrapper<CompanySystemEmployeeDO>(){{
            eq(CompanySystemEmployeeDO::getEmployeeId,employeeId);
        }});
    }
}
